/**
 * @Aurhor 把手给我
 * @Date 2022/8/30 9:24
 * @Version 1.0
 * 聊天封装消息的工具类
 **/
package com.ft.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ft.domain.ResultMessage;

public class MessageUtils {
    public static String getMessage(boolean isSystem,String forname,Object message){
        try {
            //创建服务器端消息类对象
            ResultMessage res = new ResultMessage();
            res.setSystem(isSystem);
            res.setMessage(message);
            //判断是否是给用户发送的信息
            if(forname != null){
                res.setForname(forname);
            }
            //创建处理json数据的ObjectMapper类
            ObjectMapper mapper = new ObjectMapper();
            //json将对象转换成字符串
            return mapper.writeValueAsString(res);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
